﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NoORM.Core.ServiceScript
{
    /// <summary>
    /// 服务端变量
    /// </summary>
    public class ServiceVariable
    {
        /// <summary>
        /// 用服务端的值替换字典中的变量
        /// </summary>
        /// <param name="dictionary"></param>
        public static void Replace(Dictionary<string, object> dictionary)
        {
            if (dictionary == null)
            {
                return;
            }
            foreach(var item in dictionary)
            {
                if ("$GUID".Equals(item.Value))
                {
                    dictionary[item.Key] = Guid.NewGuid().ToString();
                }
                if ("$NOW".Equals(item.Value))
                {
                    dictionary[item.Key] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if ("$TICKS".Equals(item.Value))
                {
                    dictionary[item.Key] = DateTime.Now.Ticks.ToString();
                }
            }
        }

        /// <summary>
        /// 用服务端的值替换字典中的变量
        /// </summary>
        /// <param name="dictionary"></param>
        public static void Replace(Dictionary<string, string> dictionary)
        {
            if (dictionary == null)
            {
                return;
            }
            foreach (var item in dictionary)
            {
                if ("$GUID".Equals(item.Value))
                {
                    dictionary[item.Key] = Guid.NewGuid().ToString();
                }
                if ("$NOW".Equals(item.Value))
                {
                    dictionary[item.Key] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if ("$TICKS".Equals(item.Value))
                {
                    dictionary[item.Key] = DateTime.Now.Ticks.ToString();
                }
            }
        }
    }
}
